package com.geese.module.ceo.service;

import com.geese.common.constants.BusinessConstants;
import com.geese.common.entity.Courier;
import com.geese.common.entity.School;
import com.geese.common.util.PageUtil;
import com.geese.common.vo.Page;
import com.geese.common.vo.Paginator;
import com.geese.exception.BusinessException;
import com.geese.module.admin.vo.SchoolQuery;
import com.geese.module.ceo.dao.BuildingMapper;
import com.geese.module.ceo.dao.CampusMapper;
import com.geese.module.ceo.dao.CourierMapper;
import com.geese.module.ceo.dao.SchoolMapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author YeFangYi
 */
@Service
public class SchoolService {

	@Autowired
	private SchoolMapper schoolMapper;
	@Autowired
	private CampusMapper campusMapper;
	@Autowired
	private BuildingMapper buildingMapper;
	@Autowired
	private CourierMapper courierMapper;

	/**
	 * 保存学校
	 */
	public void save(School school) {
		School codeSchool = schoolMapper.findByCode(school.getCode());
		if (codeSchool != null) {
			throw new BusinessException(120053);
		}
		School nameSchool = schoolMapper.findByName(school.getName());
		if (nameSchool != null) {
			throw new BusinessException(120052);
		}
		school.setDisplayable(1);
		school.setEnable(1);
		schoolMapper.save(school);
	}

	/**
	 * 编辑学校
	 */
	public void update(School school) {
		School model = schoolMapper.findById(school.getId());
		if (model == null) {
			throw new BusinessException(120049);
		}
		School codeSchool = schoolMapper.findByCode(school.getCode());
		if (codeSchool != null && !Objects.equals(model.getId(), codeSchool.getId())) {
			throw new BusinessException(120053);
		}
		School nameSchool = schoolMapper.findByName(school.getName());
		if (nameSchool != null && !Objects.equals(model.getId(), nameSchool.getId())) {
			throw new BusinessException(120052);
		}
		model.setCode(school.getCode());
		model.setName(school.getName());
		model.setManagerName(school.getManagerName());
		model.setManagerMobile(school.getManagerMobile());
		model.setCardinalNumber(school.getCardinalNumber());
		schoolMapper.update(model);
	}

	/**
	 * 删除学校
	 */
	public void batchRemove(List<Integer> schoolIds) {
		List<School> schoolList = schoolMapper.listByIds(schoolIds);
		if (CollectionUtils.isEmpty(schoolList) || schoolList.size() != schoolIds.size()) {
			throw new BusinessException(120051);
		}
		List<Courier> courierList = courierMapper.listBySchoolIds(schoolIds, BusinessConstants.CEO);
		if (CollectionUtils.isNotEmpty(courierList)) {
			throw new BusinessException(120050);
		}
		schoolMapper.batchRemove(schoolIds);
		campusMapper.batchRemoveBySchoolIds(schoolIds);
		buildingMapper.batchRemoveBySchoolIds(schoolIds);
	}

	/**
	 * 全部学校列表
	 */
	public Paginator<School> listAllByQuery(SchoolQuery query, Page page) {
		PageHelper.startPage(page.getCurrPage(), page.getPageSize(), " id DESC");
		List<School> schoolList = schoolMapper.listAllByQuery(query.getCode(), query.getName(), query.getManagerMobile());
		return PageUtil.getPage(schoolList);
	}

	/**
	 * 获取全部学校
	 */
	public List<School> listAll() {
		return schoolMapper.listAll();
	}

}
